package com.goodl.service.user;

import com.goodl.dao.BaseDao;
import com.goodl.dao.user.UserDao;
import com.goodl.dao.user.UserDaoImpl;
import com.goodl.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    public User login(String userCode, String password) throws Exception {
        Connection connection = null;
        User user = null;
        try {

            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public boolean updatePwd(int id, String newPwd) throws Exception {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection, id, newPwd) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Test
    public void test() throws Exception {
        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.login("admin", "123456");
        System.out.printf(user.getUserPassword());
    }
}
